﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace PKSoftManagement
{
    public partial class TiepNhanKH : UserControl
    {
        BusinessLogicLayer.HoSoBenhNhanBLL BLL;
        BindingSource All;
        public TiepNhanKH()
        {
            InitializeComponent();
            DGView.AutoGenerateColumns = false;
            All = new BindingSource();
            BLL = new BusinessLogicLayer.HoSoBenhNhanBLL();
            All.DataSource = BLL.GetAll().ToList();
            DGView.DataSource = All;
            switchEnable(true);
        }

        private void Search_Click(object sender, EventArgs e)
        {
            if (kContent.Text != string.Empty)
            {
                All.DataSource = BLL.GetAll().ToList();
                var a = All.List.OfType<DataTransferObject.HoSoBenhNhanDto>().ToList().FindAll(f => f.Id == int.Parse(kContent.Text));
                DGView.DataSource = a;
            }
            else
            {
                All.DataSource = BLL.GetAll().ToList();
                DGView.DataSource = All;
            }
        }
        private void BT_Click(object sender, EventArgs e)
        {
            switch (((ButtonX)sender).Name)
            {
                case "add": SaveFunct(); switchEnable(false); break;
                case "delete": DeleteFunct(); break;
                case "trieuChung": spInfo(); break;
                case "benhAn": break;
            }
        }

        private void DGView_SelectionChanged(object sender, EventArgs e)
        {
            if (DGView.CurrentRow != null)
            {
                switchEnable(true);
                soThe.Text = (DGView.CurrentRow.Cells[0].Value == null
                    ? string.Empty : DGView.CurrentRow.Cells[0].Value.ToString());
                tenBenhNhan.Text = (DGView.CurrentRow.Cells[1].Value == null
                    ? string.Empty : DGView.CurrentRow.Cells[1].Value.ToString()) + " " +
                     (DGView.CurrentRow.Cells[2].Value == null
                    ? string.Empty : DGView.CurrentRow.Cells[2].Value.ToString());
                diaChi.Text = DGView.CurrentRow.Cells[3].Value == null
                    ? string.Empty : DGView.CurrentRow.Cells[3].Value.ToString();
                dienThoai.Text = DGView.CurrentRow.Cells[4].Value == null
                    ? string.Empty : DGView.CurrentRow.Cells[4].Value.ToString();
                gioiTinh.SelectedItem = DGView.CurrentRow.Cells[5].Value == null
                    ? null : ((bool)DGView.CurrentRow.Cells[5].Value == true ? gioiTinh.Items[0] : gioiTinh.Items[1]);
                eMail.Text = DGView.CurrentRow.Cells[6].Value == null
                    ? string.Empty : DGView.CurrentRow.Cells[6].Value.ToString();
                namSinh.Value = DGView.CurrentRow.Cells[7].Value == null
                    ? (System.DateTime.Today) : (System.DateTime)DGView.CurrentRow.Cells[7].Value;
            }
        }
        private void kContent_TextChanged(object sender, EventArgs e)
        {
            if (kContent.Text == string.Empty)
            {
                All.DataSource = BLL.GetAll().ToList();
                DGView.DataSource = All;
            }
        }
        #region
        private void SaveFunct()
        {
            switchEnable(false);
            soThe.Text = string.Empty;
            namSinh.Text = string.Empty;
            tenBenhNhan.Text = string.Empty;
            diaChi.Text = string.Empty;
            gioiTinh.SelectedItem = null;
            eMail.Text = string.Empty;
            hoBenhNhan.Text = string.Empty;
            dienThoai.Text = string.Empty;
        }
        private void DeleteFunct()
        {
            try
            {
                if (DGView.SelectedRows.Count > 0)
                {
                    BLL.Delete((int)DGView.SelectedRows[0].Cells["ID"].Value);
                    All.DataSource = BLL.GetAll().ToList();
                    DGView.DataSource = All;
                    MessageBox.Show("Đã Xóa Thành Công");
                }
                else
                {
                    MessageBox.Show("Bạn phải chọn đối tượng để xóa");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void spInfo()
        {
            Control temp = null;
            if (!soThe.Text.Equals(string.Empty))
            {
                temp = splitContainer1.Panel2.Controls.Find(soThe.Text, true).FirstOrDefault();
                if (temp == null)
                {
                    MDIForm mdi = new MDIForm(int.Parse(soThe.Text), tenBenhNhan.Text)
                    {
                        Name = soThe.Text,
                        TopLevel = false,
                        Text = soThe.Text + ": " + tenBenhNhan.Text,
                        Tag = int.Parse(soThe.Text)
                    };
                    this.splitContainer1.Panel2.Controls.Add(mdi);
                    mdi.BringToFront();
                    mdi.Show();
                }
                else
                {
                    if ((temp as Form).WindowState == FormWindowState.Minimized)
                        (temp as Form).WindowState = FormWindowState.Normal;
                    temp.BringToFront();
                }
            }
        }
        private void switchEnable(bool bl)
        {
            foreach (TextBox tb in splitContainer1.Panel1.Controls.OfType<TextBox>())
            {
                if(tb != soThe)
                tb.ReadOnly = bl;
            }
            hoBenhNhan.Enabled = !bl;
            gioiTinh.Enabled = !bl;
            namSinh.Enabled = !bl;
            add.Enabled = bl;
            delete.Enabled = bl;
            trieuChung.Enabled = bl;
            ok.Enabled = !bl;
            cancel.Enabled = !bl;
        }
        #endregion

        private void ok_Click(object sender, EventArgs e)
        {
            BLL.Insert(new DataTransferObject.HoSoBenhNhanDto() {
                Ten=tenBenhNhan.Text,
                DiaChi=diaChi.Text,
                DienThoai=dienThoai.Text,
                GioiTinh=gioiTinh.SelectedItem.ToString()=="Nam"?true:false,
                Email=eMail.Text,
                NamSinh=namSinh.Value,
                Ho=hoBenhNhan.Text
            });
            All.DataSource = BLL.GetAll().ToList();
            DGView.DataSource = All;
            MessageBox.Show("Lưu Thành Công");
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            switchEnable(true);
        }
    }
}
